package cn.edu.bupt.ch9_1_publisher.controller;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Tag(name = "发送消息")
@RestController
@RequestMapping("/api/v1")
public class SendMessageController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Operation(summary = "发送DirectMessage")
    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage(String msg){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = msg;
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "ok";

   }

    @Operation(summary = "发送TopicMessage")
    @GetMapping("/sendTopicMessage")
    public String sendTopicMessage(String msg,String routingKey){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = msg;
        String createTime = LocalDateTime.now()
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",messageData);
        map.put("createTime",createTime);
        //将消息设置路由键并发送到交换机topicExchange
        rabbitTemplate.convertAndSend("topicExchange", routingKey, map);
        return "ok";
    }
}
